﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class IntegralDetails
    {
        private readonly KeRui.DAL.IntegralDetails dal = new KeRui.DAL.IntegralDetails();

        public int Add(KeRui.Model.IntegralDetails model)
        {
            return this.dal.Add(model);
        }

        public List<KeRui.Model.IntegralDetails> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.IntegralDetails> list = new List<KeRui.Model.IntegralDetails>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.IntegralDetails item = new KeRui.Model.IntegralDetails();
                    if (dt.Rows[i]["Id"].ToString() != "")
                    {
                        item.Id = int.Parse(dt.Rows[i]["Id"].ToString());
                    }
                    if (dt.Rows[i]["UserId"].ToString() != "")
                    {
                        item.UserId = int.Parse(dt.Rows[i]["UserId"].ToString());
                    }
                    item.UserName = dt.Rows[i]["UserName"].ToString();
                    if (dt.Rows[i]["Type"].ToString() != "")
                    {
                        item.Type = int.Parse(dt.Rows[i]["Type"].ToString());
                    }
                    if (dt.Rows[i]["Integrals"].ToString() != "")
                    {
                        item.Integrals = int.Parse(dt.Rows[i]["Integrals"].ToString());
                    }
                    if (dt.Rows[i]["HandleDate"].ToString() != "")
                    {
                        item.HandleDate = DateTime.Parse(dt.Rows[i]["HandleDate"].ToString());
                    }
                    item.Remark = dt.Rows[i]["Remark"].ToString();
                    list.Add(item);
                }
            }
            return list;
        }

        public void Delete(int Id)
        {
            this.dal.Delete(Id);
        }

        public int GetCount(string strWhere)
        {
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetCount("IntegralDetails", strWhere);
        }

        public List<KeRui.Model.IntegralDetails> GetList(string strCondition)
        {
            string strWhere = "";
            string strOrder = "";
            int pageSize = -1;
            int pageIndex = -1;
            DataSet set = new KeRui.DAL.Common().GetList("IntegralDetails", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.IntegralDetails> GetList(int PageSize, int PageIndex, string strWhere, string strOrder)
        {
            DataSet set = new KeRui.DAL.Common().GetList("IntegralDetails", PageSize, PageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.IntegralDetails> GetListByCache(string strCondition)
        {
            string cacheKey = "IntegralDetailsList-" + strCondition;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetList(strCondition);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (List<KeRui.Model.IntegralDetails>) cache;
        }

        public KeRui.Model.IntegralDetails GetModel(int Id)
        {
            return this.dal.GetModel(Id);
        }

        public void Update(KeRui.Model.IntegralDetails model)
        {
            this.dal.Update(model);
        }
    }
}

